package com.xprime.mobileFramework.file;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.adobe.fre.FREContext;

public class FileOpen {
	
	public void open(Context context , String fileName, String fullFilePath, FREContext eventDispatcher )
	{
		Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		
		String path = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();		
		fullFilePath = path + "/" + fullFilePath;
		
        File file;
		try {
			file = new File( fullFilePath );
			
			if( file.exists()==false)
			{
				Toast.makeText(context, fullFilePath + " 파일이 없습니다.", 5000).show();
				if( eventDispatcher != null )
				{
					eventDispatcher.dispatchStatusEventAsync("fileOpenExtension", fullFilePath );
				}
				
				return;
			}
			
			if (file.getName().endsWith(".pdf")){
	            intent.setDataAndType(Uri.fromFile(file), "application/pdf");
	        }else if (file.getName().endsWith(".hwp")){
	            intent.setDataAndType(Uri.fromFile(file), "application/hwp");
	        }else if (file.getName().endsWith(".xls")){
	            intent.setDataAndType(Uri.fromFile(file), "application/vnd.ms-excel");
	        }else if (file.getName().endsWith(".xlsx")){
	        	intent.setDataAndType(Uri.fromFile(file), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
	        }else if (file.getName().endsWith(".pptx")){
	            intent.setDataAndType(Uri.fromFile(file), "application/vnd.openxmlformats-officedocument.presentationml.presentation");
	        }else if (file.getName().endsWith(".ppt")){
	            intent.setDataAndType(Uri.fromFile(file), "application/vnd.ms-powerpoint");    
	        }else if (file.getName().endsWith(".docx")){
	            intent.setDataAndType(Uri.fromFile(file), "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
	        }else if (file.getName().endsWith(".doc")){
	            intent.setDataAndType(Uri.fromFile(file), "application/msword");
	        }else if(file.getName().endsWith("gul")){
	        	intent.setDataAndType(Uri.fromFile(file), "application/gul");
	        }else if(file.getName().endsWith("mp4")){
	        	intent.setDataAndType(Uri.fromFile(file), "video/mp4");
	        }else if(file.getName().endsWith("avi")){
	        	intent.setDataAndType(Uri.fromFile(file), "video/x-msvideo");
	        }else if(file.getName().endsWith("mov")){
	        	intent.setDataAndType(Uri.fromFile(file), "video/quicktime");
	        }
			
			try
			{
				Toast.makeText( context, fileName, 5000 ).show();
				context.startActivity(intent);
			} 
			catch(Exception e) 
			{
				Log.e("FileManager : ", e.getMessage());
				Toast.makeText(context, "실행 어플이 없습니다. 마켓으로 이동합니다.", 10000).show();
				String fileID = null;
				if (file.getName().endsWith(".hwp")){
					fileID = "kr.co.hancom.hancomviewer.androidmarket";
				}else if(file.getName().endsWith(".avi")){
					fileID = "com.dgo.VitalPlayer";
				}else if(file.getName().endsWith(".gul")){
					Toast.makeText(context, "훈민정음 앱은 안드로이드 마켓에 없습니다. 위 사이트에서 설치해주시기 바랍니다.", 10000).show();
					String gulPath = "http://jungum.com/html/kr/02_JuMobile/02_01_JuMobile_History_list.html?part=JuMobile&sec=1&c=0&BBSCD=002015000&Category=&what_s=&schtext=&pagesize=10&page=2";
					intent.setData(Uri.parse(gulPath));
					context.startActivity(intent);
				}
				
				if( fileID != null )
				{
					Intent intent2 = new Intent(Intent.ACTION_VIEW, Uri.parse("http://market.android.com/details?id=" + fileID ));
					context.startActivity(intent2);
				}
			}
			
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} 
	}
}







